<template>
  <div class="tinymce-editor-container">
    <editor
      v-if="!reloading"
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick">
    </editor>
  </div>
</template>


<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver/theme'
import 'tinymce/plugins/image'
import 'tinymce/plugins/link'
import 'tinymce/plugins/media'
import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/wordcount'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'
import 'tinymce/plugins/fullscreen'
import 'tinymce/icons/default'
import { uploadAction, getFileAccessHttpUrl } from '@/api/manage'
import { getVmParentByName } from '@/utils/util'

export default {
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      required: false
    },
    triggerChange: {
      type: Boolean,
      default: false,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists image link media table textcolor wordcount contextmenu fullscreen'
    },
    toolbar: {
      type: [String, Array],
      default: 'undo redo | formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists link unlink image media table | removeformat | fullscreen',
      branding: false
    }
  },
  data() {
    return {
      init: {
        language_url: '/tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        skin_url: '/tinymce/skins/lightgray',
        height: 300,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        toolbar_drawer: false,
        images_upload_handler: (blobInfo, success) => {
          let formData = new FormData()
          formData.append('file', blobInfo.blob(), blobInfo.filename())
          formData.append('biz', "jeditor")
          formData.append("jeditor", "1")
          uploadAction(window._CONFIG['domianURL'] + "/sys/common/upload", formData).then((res) => {
            if (res.success) {
              if (res.message == 'local') {
                const img = 'data:image/jpeg;base64,' + blobInfo.base64()
                success(img)
              } else {
                let img = getFileAccessHttpUrl(res.message)
                success(img)
              }
            }
          })
        }
      },
      myValue: this.value,
      reloading: false,
    }
  },
  mounted() {
    this.initATabsChangeAutoReload()
  },
  methods: {
    reload() {
      this.reloading = true
      this.$nextTick(() => this.reloading = false)
    },
    onClick(e) {
      this.$emit('onClick', e, tinymce)
    },
    clear() {
      this.myValue = ''
    },
    initATabsChangeAutoReload() {
      let tabs = getVmParentByName(this, 'ATabs')
      let tabPane = getVmParentByName(this, 'ATabPane')
      if (tabs && tabPane) {
        let currentKey = tabPane.$vnode.key
        tabs.$on('change', (key) => {
          if (currentKey === key) {
            this.reload()
          }
        })
        this.reload()
      } else {
        let tabLayout = getVmParentByName(this, 'TabLayout')
        try {
          tabLayout.excuteCallback(() => {
            this.reload()
          })
        } catch (error) {
          if (tabLayout) {
            this.reload()
          }
        }
      }
    }
  },
  watch: {
    value(newValue) {
      this.myValue = (newValue == null ? '' : newValue)
    },
    myValue(newValue) {
      if (this.triggerChange) {
        this.$emit('change', newValue)
      } else {
        this.$emit('input', newValue)
      }
    }
  }
}
</script>


<style scoped>
.tinymce-editor-container {
  pointer-events: auto;
}

/* 禁用编辑器内容但允许滚动 */
.jeecg-form-container-disabled .tox .tox-edit-area__iframe {
  pointer-events: none;
}

/* 允许滚动事件 */
.jeecg-form-container-disabled .tox .tox-edit-area__iframe {
  pointer-events: auto !important;
  overflow: auto !important;
  -ms-user-select: none;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
}

/* 禁用编辑器内容区域的编辑功能 */
.jeecg-form-container-disabled .tox .tox-edit-area {
  pointer-events: none;
}
</style>


